package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.EquityPropsDao;
import com.sc.nft.dao.MallActivityExchangeDao;
import com.sc.nft.dao.MallRecommendDao;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.EquityProps;
import com.sc.nft.entity.MallActivity;
import com.sc.nft.entity.MallActivityExchange;
import com.sc.nft.entity.vo.mall.ActivityExchangeVO;
import com.sc.nft.entity.vo.mall.RecommendActivityPageVO;
import com.sc.nft.enums.MallGoodsLabelEnums;
import com.sc.nft.enums.ProductTypeEnum;
import com.sc.nft.enums.task.ActivityTypeEnum;
import com.sc.nft.enums.task.ParticipantsEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.MallRecommendService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 推荐商品表服务接口实现
 *
 * @author zxy
 * @description 活动推荐表
 * @since 2023-05-15 15:26:29
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallRecommendServiceImpl implements MallRecommendService {
    private final MallRecommendDao mallRecommendDao;
    private final MallActivityExchangeDao exchangeDao;
    private final EquityPropsDao equityPropsDao;
    private final DigitalCollectionDao digitalCollectionDao;

    @Override
    public IPage<RecommendActivityPageVO> recommendPage(ActivityTypeEnum activityType, ProductTypeEnum type,String productName, Page page) {
        IPage<RecommendActivityPageVO> recommendActivityPageVOIPage = mallRecommendDao.recommendPage(activityType, type, productName,page);
        if (CollUtil.isNotEmpty(recommendActivityPageVOIPage.getRecords())) {
            for (RecommendActivityPageVO record : recommendActivityPageVOIPage.getRecords()) {
                if (record.getSpecsCount() > 1) {
                    record.setSalesPrice(record.getSalesPrice() + "起");
                }
                if(StrUtil.isNotBlank(record.getCoverImage())){
                    record.setCoverImage(StrUtil.split(record.getCoverImage(),",").get(0));
                }
                List<MallActivityExchange> activityExchanges = exchangeDao.getByActivityId(record.getActivityId());
                List<ActivityExchangeVO> exchangeVOS = Lists.newArrayList();
                for (MallActivityExchange activityExchange : activityExchanges) {
                    ActivityExchangeVO activityExchangeVO = new ActivityExchangeVO();
                    activityExchangeVO.setType(1);
                    switch (activityExchange.getType()) {
                        case INTEGRAL:
                            activityExchangeVO.setName("星球碎片");
                            break;
                        case EQUITY:
                            EquityProps equityProps = equityPropsDao.getById(activityExchange.getLinkId());
                            activityExchangeVO.setName(equityProps.getFirstTitle());
                            break;
                        case NFT:
                            DigitalCollection collection = digitalCollectionDao.getById(activityExchange.getLinkId());
                            activityExchangeVO.setName(collection.getFirstTitle());
                            break;
                    }
                    activityExchangeVO.setCount(activityExchange.getCostNum());
                    exchangeVOS.add(activityExchangeVO);
                }
                if(record.getParticipants()== ParticipantsEnum.APPOINT){
                    ActivityExchangeVO activityExchangeVO = new ActivityExchangeVO();
                    activityExchangeVO.setType(2);
                    DigitalCollection collection = digitalCollectionDao.getById(record.getCollectionId());
                    activityExchangeVO.setName(collection.getFirstTitle());
                    activityExchangeVO.setCount(1);
                    exchangeVOS.add(activityExchangeVO);
                }
                record.setActivityExchanges(exchangeVOS);
                //判断标签
                if (record.getLabelType() != null) {
                    if (record.getLabelType() == MallGoodsLabelEnums.NEW_LABEL) {
                        //新品，判断商品的创建时间是否大于标签的创建时间
                        if (record.getLabelStartTime().after(record.getCreateTime()) || !DateUtil.isIn(new Date(),record.getLabelStartTime(),record.getLabelEndTime())) {
                            record.setLabelImage(null);
                        }
                    }else if (record.getLabelType() == MallGoodsLabelEnums.ACTIVITY_LABEL) {
                        //活动
                        if (!DateUtil.isIn(new Date(),record.getLabelStartTime(),record.getLabelEndTime())) {
                            record.setLabelImage(null);
                        }
                    }
                }
            }
        }
        return recommendActivityPageVOIPage;
    }
}